package fonkin.com.wifisignapp.acticity.addressList;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SearchView;

import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import fonkin.com.wifisignapp.R;
import fonkin.com.wifisignapp.acticity.proceeding.DeptUserDetail;
import fonkin.com.wifisignapp.adapter.news.UserAdapter;
import fonkin.com.wifisignapp.bean.user.User;
import fonkin.com.wifisignapp.retrofit.BaseRetrofit;
import fonkin.com.wifisignapp.utils.ReturnVo;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SearchUser extends AppCompatActivity implements SearchView.OnQueryTextListener  {

    SearchView searchContent;
    ImageView searchBtn,bgImage;
    UserAdapter userAdapter;
    ListView userListView;
    List<User> users = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_user);
        searchBtn = (ImageView)findViewById(R.id.search_btn);
        bgImage = (ImageView)findViewById(R.id.bg);
        bgImage.setVisibility(View.GONE);
        searchContent = (SearchView) findViewById(R.id.search_content);
        int magId = getResources().getIdentifier("android:id/search_mag_icon",null, null);
        ImageView magImage = (ImageView) searchContent.findViewById(magId);
        magImage.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
        searchContent.setOnQueryTextListener(this);
        userListView = (ListView)findViewById(R.id.search_user_listview);

    }

    public void initData(String name){
        BaseRetrofit.GetBaseRetrofit().userAjaxList(name).enqueue(new Callback<ReturnVo<List<User>>>() {
            @Override
            public void onResponse(Call<ReturnVo<List<User>>> call, Response<ReturnVo<List<User>>> response) {
                if (response.isSuccessful()&&response.body().getCode().equals(1)) {
                    userAdapter = new UserAdapter(SearchUser.this, new ArrayList<User>());
                    userListView.setAdapter(userAdapter);
                    users = response.body().getData();
                    if (!users.isEmpty()) {
                        bgImage.setVisibility(View.GONE);
                        userAdapter.addAll(users);
                        userListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                User userDetail = users.get(position);
                                Intent intent = new Intent(SearchUser.this, DeptUserDetail.class);
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("userDetail", (Serializable) userDetail);
                                intent.putExtras(bundle);
                                startActivity(intent);

                            }
                        });
                    }else{
                        bgImage.setVisibility(View.VISIBLE);
                        Picasso
                                .with(SearchUser.this)
                                .load(R.drawable.none_data)
                                .resize(500,400)
                                .into(bgImage);
                    }
                }
            }

            @Override
            public void onFailure(Call<ReturnVo<List<User>>> call, Throwable t) {

            }
        });

    }


    public void searchUser(View v){

    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        initData(newText);
        Log.d("=============",newText);
        return false;

    }
}
